<template>
    <div>
        <div>
            <div style="visibility:hidden">
                <Input style="width:250px" v-model="signature">
                <span slot="prepend">签名信息：</span>
                <Button slot="append" icon="ios-search" @click='findByInsName'></Button>
                </Input>
            </div>
        </div>
    
        <Table border stripe :columns="header" :data="data_list" size="small" ref="table" class="table"></Table>
    
        <Modal v-model="modal_del_status" title="删除一个短信签名" @on-ok="option_delete_save" @on-cancel="modal_cancel">
            <Form :model="temp_dele_data" :label-width="80">
                <p>您确定删除这个记录吗？</p>
            </Form>
        </Modal>
    
    </div>
</template>
<script>
export default {
    data() {
        return {

            //初始化url
            init_url: '/mes/core/smsTemp/real/app/temp/get/list/',
            del_url: '/mes/core/smsTemp/real/delete/',

            //表格标题
            header: [
                {
                    type: 'index',
                    width: 60,
                    align: 'center'
                },
                {
                    "title": "id",
                    "key": "id",
                    "width": 150,
                    "sortable": true
                },
                {
                    "title": "模板名称",
                    "key": "name",
                    "width": 120,
                    "sortable": true,
                    render: (h, params) => {
                        return h('div', {
                        }, params.row.baseSmsTemplateEntity.name)
                    }
                },
                {
                    "title": "模板code",
                    "key": "code",
                    "width": 120,
                    "sortable": true,
                    render: (h, params) => {
                        return h('div', {
                        }, params.row.baseSmsTemplateEntity.code)
                    }
                },
                {
                    "title": "模板类型",
                    "key": "templateType",
                    "width": 120,
                    "sortable": true,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', {
                        }, params.row.baseSmsTemplateEntity.templateType)
                    }
                },
                {
                    "title": "模板内容",
                    "key": "content",
                    "width": 250,
                    render: (h, params) => {
                        return h('div', {
                        }, params.row.baseSmsTemplateEntity.content)
                    }
                },
                {
                    "title": '操作',
                    "min-width": 100,
                    "align": 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'error',
                                    size: 'small'
                                },
                                on: {
                                    click: () => {
                                        this.delete_show(params)
                                    }
                                }
                            }, '删除')
                        ]);
                    }
                }
            ],
            ruleValidate: {
                name: [
                    { required: true, message: '姓名不能为空', trigger: 'blur' }
                ],
            },
            //表格数据源
            data_list: [],
            //表格数据源缓存
            data_cache_list: [],
            //选中的数据的应用id集合
            data_select_list: [],
            //查询的名称
            signature: '',

            modal_del_status: false,
            temp_dele_data: {},

        }
    },
    methods: {
        //到处excel
        exportData(type) {
            if (type === 1) {
                this.$refs.table.exportCsv({
                    filename: '原始数据'
                });
            } else if (type === 2) {
                this.$refs.table.exportCsv({
                    filename: '排序和过滤后的数据',
                    original: false
                });
            }
        },

        //根据学院名称查询学院
        findByInsName() {
            this.signature = this.$stringUtil.stringTrim(this.signature);
            var signature = this.signature;

            if (signature == "") {
                this.data_list = this.data_cache_list.slice();
                return;
            }

            var match = this.$stringUtil.stringMatch;
            this.data_list = $.grep(this.data_cache_list, function (value) {
                if (match(value.signature, signature)) {
                    return value;
                }
            });
        },

        appinit(_self) {
            var _self = this;
            this.$ajax.get(_self.init_url + _self.appId)
                .then(function (response) {
                    if (response.data.status == "success") {
                        _self.data_list = response.data.data;
                        _self.data_cache_list = _self.data_list.slice();
                    }

                })
                .catch(function (error) {
                    console.log(error);
                });
        },
        delete_show(params) {
            this.modal_del_status = true;
            this.temp_dele_data = params.row;
        },
        modal_cancel() {
            this.$Message.info("操作取消");
        },
        option_delete_save() {
            var _self = this;
            console.log(_self.temp_dele_data);
            var func = this.appinit;
            this.$Message.info("操作确认");
            this.$ajax.post(_self.del_url + _self.temp_dele_data.id, )
                .then(function (response) {
                    if (response) {
                        if (response.data.status == 'success') {
                            _self.$Message.info('删除成功');
                            func(_self);
                            return;
                        }
                        _self.$Message.info('删除失败');
                    }
                    _self.$Message.info('删除失败');
                })
                .catch(function (error) {
                    console.log(error);
                    _self.$Message.info('删除失败');
                });
        }


    },
    mounted() {

        var _self = this;

        //初始化表单数据
        // this.$ajax.get(_self.init_url)
        //     .then(function (response) {
        //         _self.data_list = response.data;
        //         _self.data_cache_list = _self.data_list.slice();
        //     })
        //     .catch(function (error) {
        //         console.log(error);
        //     });
    },
    props: ["appId"],
    watch: {
        appId: function (newValue, oldValue) {
            var _self = this;
            if (newValue == oldValue) {
                return;
            }
            //初始化表单数据
            this.$ajax.get(_self.init_url + newValue)
                .then(function (response) {
                    if (response.data.status == "success") {
                        _self.data_list = response.data.data;
                        _self.data_cache_list = _self.data_list.slice();
                    }

                })
                .catch(function (error) {
                    console.log(error);
                });
        }
    }
}
</script>

<style>
.btn-group {
    margin-bottom: 5px;
}
</style>
